package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.OpinfoService;

import net.sf.json.JSONObject;
@Controller
@RequestMapping("/opinfo")
public class OpinfoController extends JsonController{
	@Autowired
	private OpinfoService opinfoService;
	/**
	 * @作者 js  
	 * @方法名 queryOpinfoList  
	 * @功能 TODO 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryOpinfoList")
	public ModelAndView queryOpinfoList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "START_DATE");
		String endDate = RequestUtils.getStringParameter(request, "END_DATE");
		String username = RequestUtils.getStringParameter(request, "USER_NAME");
		String deviceType = RequestUtils.getStringParameter(request, "DEVICE_TYPE");
		String deviceCityCode = RequestUtils.getStringParameter(request, "DEVICE_CITY_CODE");
		String deviceOperType = RequestUtils.getStringParameter(request, "DEVICE_OPER_TYPE");
		params.put("start", start);
		params.put("limit", limit);
		params.put("START_DATE", startDate);
		params.put("END_DATE", endDate);
		params.put("USER_NAME", username);
		params.put("DEVICE_TYPE", deviceType);
		params.put("DEVICE_CITY_CODE", deviceCityCode);
		params.put("DEVICE_OPER_TYPE", deviceOperType);
		List<?> list = opinfoService.queryOpinfoList(params);
		int count = opinfoService.getOpinfoCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
			
		}
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryAppLoadList  
	 * @功能 TODO 获取App首次登陆 日志 列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryAppLoadList")
	public ModelAndView queryAppLoadList(HttpServletRequest request,HttpServletResponse response)throws Exception {
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String sblx = RequestUtils.getStringParameter(request, "sblx");
		String dlcs = RequestUtils.getStringParameter(request, "dlcs");
		String oadq = RequestUtils.getStringParameter(request, "oadq");
		String tjr = RequestUtils.getStringParameter(request, "tjr");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("yhm", yhm);
		params.put("sblx", sblx);
		params.put("dlcs", dlcs);
		params.put("oadq", oadq);
		params.put("tjr", tjr);
		List<Map<String, Object>> list=opinfoService.queryAppLoadList(params);
		int count=opinfoService.getAppLoadCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
			
		}
		serialize(request, response, result);
		return null;
	}
	
	/**
	 * @作者 js  
	 * @方法名 exportAppLoginTjExcelMethod  
	 * @功能 TODO 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("exportAppLoadExcelMethod")
	public ModelAndView exportAppLoginTjExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String sblx = RequestUtils.getStringParameter(request, "sblx");
		String dlcs = RequestUtils.getStringParameter(request, "dlcs");
		String oadq = RequestUtils.getStringParameter(request, "oadq");
		String tjr = RequestUtils.getStringParameter(request, "tjr");
		JSONObject params = new JSONObject();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("yhm", yhm);
		params.put("sblx", sblx);
		params.put("dlcs", dlcs);
		params.put("oadq", oadq);
		params.put("tjr", URLDecoder.decode(tjr));
		
		// 查询数据
		List list = opinfoService.queryAppLoadList(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getAppLoadExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"App首次登录信息");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "App首次登录信息.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 getAppLoginTj  
	 * @功能 TODO 地区统计数据
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getAppLoginTj")
	public ModelAndView getAppLoginTj(HttpServletRequest request,HttpServletResponse response)throws Exception {
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String roleParam = RequestUtils.getStringParameter(request, "roleParam");
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("roleParam", roleParam);
		List<Map<String, Object>> list=opinfoService.getAppLoginTj(params);
		result.put("data", list);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 exportAppLoginiTjExcelMethod  
	 * @功能 TODO 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("exportAppLoginiTjExcelMethod")
	public ModelAndView exportAppLoginiTjExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String roleParam = RequestUtils.getStringParameter(request, "roleParam");
		JSONObject params = new JSONObject();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("roleParam", URLDecoder.decode(roleParam));
		
		// 查询数据
		List list = opinfoService.getAppLoginTj(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getAppLoginiTjExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(startDate+"至"+endDate);
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "App首次登录统计信息.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
}


